package strategies;

import utils.Sorting;
import exceptions.DatabaseException;
import general.IContext;
import java.util.*;
import wrappers.AbstractWrap;

/**
 *
 * @author Alejandro
 */
public class ViewTableStrategy extends AbstractStrategy {

    @Override
    public void executeOperation(IContext context) {
        prepareContext(context);
        if ((dao != null)) {
            List<Object> list = null;
            try {
                list = dao.selectAll();
                if (pp.getParamMap().containsKey("search")) {
                    if (pp.getParamMap().containsKey("searchbar")) {
                        if (!pp.getParam("searchbar").isEmpty()) {
                            if (pp.getParam("searchbar").matches("[a-zA-Zа-яА-Я,; 0-9-]*")) {
                                list = dao.findByNameOrSurname(pp.getParam("searchbar"));
                            } else {
                                op.putErrorReport("Bad name or sername. You must enter expression without next symbols: '*' '%' '#' '&' '(' ')' ''' '\"' '!' '?' '/' '\\' '`' '~' ", "");
                            }
                        } else {
                            op.putErrorReport("Query is empty", "");
                        }
                    }
                }
            } catch (DatabaseException de) {
                op.putErrorReport("Selecting from database failed", de.getMessage());
            }
            if (list != null) {
                int columnNum = 0;
                Set<String> keys = pp.getParamMap().keySet();
                for (String s : keys) {
                    if (s.startsWith("qsort_")) {
                        columnNum = Integer.parseInt(s.replace("qsort_", ""));
                        break;
                    }
                }
                boolean isAscending = true;
                if (pp.getParamMap().containsKey("sorting-style")) {
                    isAscending = !Boolean.parseBoolean(pp.getParam("sorting-style"));
                    op.echo("<input type=\"hidden\" name=\"sorting-style\" value=\"" + Boolean.toString(isAscending) + "\" />");
                } else {
                    op.echo("<input type=\"hidden\" name=\"sorting-style\" value=\"true\" />");
                }
                AbstractWrap.setComparator(columnNum, isAscending);
                Sorting.sort(list);
                op.echo("<table>");
                boolean header = true;
                boolean flag = false;
                for (Object o : list) {
                    AbstractWrap e = (AbstractWrap) o;
                    if (header) {
                        op.startTableRow("head");
                        op.echo("<th></th>");
                        List<String> headers = e.getHeaders();
                        for (int i = 0; i < headers.size(); i++) {
                            op.putTableHeader("qsort_" + i, headers.get(i), pp.getParamMap().containsKey("qsort_" + i));
                        }
                        op.endTableRow();
                        header = !header;
                    }
                    op.startTableRow((flag = !flag) ? "even" : "odd");
                    op.putTableIndexCell(e.getPrimaryKey());
                    List<String> cells = e.getCells();
                    for (String s : cells) {
                        op.putTableCell(s);
                    }
                    op.endTableRow();
                }
                op.echo("</table>");
            }
        }
    }
}
